package cn.hnu.qq.server.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.server.dao.CrowdDao;

@Transactional
public class CrowdDaoImpl implements CrowdDao{

	@PersistenceContext
	private EntityManager manager;
	
	public Crowd findById(Integer Id) {
		return manager.find(Crowd.class, Id);
	}

	public Crowd findByName(String groupName) {
		String jpql = "SELECT g FROM Crowd g WHERE g.crowdName = :groupName";
		Query query = manager.createQuery(jpql);
		query.setParameter("groupName", groupName);
		return (Crowd) query.getSingleResult();
	}

	public Boolean isExist(String name) {
		if(findByName(name) != null){
			return true;
		}
		return false;
	}

	public Boolean isExist(Integer id) { 
		if(findById(id) != null){
			return true;
		}
		return false;
	}

	public EntityManager getManager() {
		return manager;
	}

	public void setManager(EntityManager manager) {
		this.manager = manager;
	}

	public void persist(Crowd group) {
		manager.persist(group);
	}

	public void merge(Crowd group) {
		manager.merge(group);
	}
	
	
}
